/**
 * @Author : czx
 * @Date : 2025/5/5
 * @Time :23:30
 */

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.awt.event.MouseEvent;
import javax.swing.JOptionPane;

/**
 * 功能：游戏逻辑<br>
 */
public class GameLogic
{
    /** 游戏面板 */
    private GamePanel gamePanel;

    /**
     * 功能：构造函数<br>
     */
    public GameLogic(GamePanel _gamePanel)
    {
        this.gamePanel = _gamePanel;
    }

    /**
     * 功能：判断下一步是白棋下还是黑棋下<br>
     */
    private int getNextChessColor()
    {
        int chessColor = -1;

        //得到上一步信息
        if(this.gamePanel.listChess.size() > 0)
        {
            Map<String,Integer> mapLast = this.gamePanel.listChess.get(this.gamePanel.listChess.size() - 1);
            if(mapLast.get("flag") == this.gamePanel.BLACKCHESS)
            {
                chessColor = this.gamePanel.WHITECHESS;
            }
            else
            {
                chessColor = this.gamePanel.BLACKCHESS;
            }
        }
        else
        {
            if(this.gamePanel.fightType == 0)	//人机对战
            {
                if(this.gamePanel.playFirst == 0)	//玩家先手
                {
                    if(this.gamePanel.chessColor == 0)
                    {
                        chessColor = this.gamePanel.BLACKCHESS;
                    }
                    else
                    {
                        chessColor = this.gamePanel.WHITECHESS;
                    }
                }
                else	//电脑先手（这是不想赢啊）
                {
                    if(this.gamePanel.chessColor == 0)
                    {
                        chessColor = this.gamePanel.WHITECHESS;
                    }
                    else
                    {
                        chessColor = this.gamePanel.BLACKCHESS;
                    }
                }
            }
            else	//人人对战
            {
                if(this.gamePanel.chessColor == 0)
                {
                    chessColor = this.gamePanel.BLACKCHESS;
                }
                else
                {
                    chessColor = this.gamePanel.WHITECHESS;
                }
            }
        }

        return chessColor;
    }

    /**
     * 功能：得到X像素所对应的竖线下标<br>
     */
    private int getLineX(int x)
    {
        //先判断靠哪条竖线近些
        int lineX = (x - this.gamePanel.chessBoradX + this.gamePanel.spaceSize / 2) / this.gamePanel.spaceSize;
        //再判断是否在有效范围内
        int posX = this.gamePanel.chessBoradX + lineX * this.gamePanel.spaceSize;
        if(x > (posX - this.gamePanel.chessSize / 2) && x < (posX + this.gamePanel.chessSize / 2)){}
        else
        {
            lineX = -1;
        }

        return lineX;
    }

    /**
     * 功能：得到Y像素所对应的横线下标<br>
     */
    private int getLineY(int y)
    {
        //先判断靠哪条竖线近些
        int lineY = (y - this.gamePanel.chessBoradY + this.gamePanel.spaceSize / 2) / this.gamePanel.spaceSize;
        //再判断是否在有效范围内
        int posY = this.gamePanel.chessBoradY + lineY * this.gamePanel.spaceSize;
        if(y > (posY - this.gamePanel.chessSize / 2) && y < (posY + this.gamePanel.chessSize / 2)){}
        else
        {
            lineY = -1;
        }

        return lineY;
    }

    /**
     * 功能：对五元组进行评分<br>
     */
    private int getTupleScore(int _computerChessNum,int _playerChessNum)
    {
        //1、该五元组中既有电脑的棋子又有玩家的棋子，大家一起完蛋，得0分。
        if(_computerChessNum > 0 && _playerChessNum > 0){return 0;}

        //2、该五元组为空，没有棋子，得7分。
        if(_computerChessNum == 0 && _playerChessNum == 0){return 7;}

        //3、该五元组中只有1个机器的棋子，其他为空，得35分。（电脑才下一个棋子，可以先看看。【优先级第七】）
        if(_computerChessNum == 1){return 35;}

        //4、该五元组中只有2个机器的棋子，其他为空，得800分。（电脑下两个棋子了。【优先级第五】）
        if(_computerChessNum == 2){return 800;}

        //5、该五元组中只有3个机器的棋子，其他为空，得15000分。（电脑看到赢的希望了，可以下这个点上，进攻进攻。【优先级第三】）
        if(_computerChessNum == 3){return 15000;}

        //6、该五元组中只有4个机器的棋子，其他为空，得800000分。（电脑马上就要赢了，无论如何也必须下这个点上，进攻进攻再进攻。【优先级第一】）
        if(_computerChessNum == 4){return 800000;}

        //7、该五元组中只有1个玩家的棋子，其他为空，得15分。（玩家才下一个棋子，别理他。【没优先级】）
        if(_playerChessNum == 1){return 15;}

        //8、该五元组中只有2个玩家的棋子，其他为空，得400分。（玩家下两个棋子了。【优先级第六】）
        if(_playerChessNum == 2){return 800;}

        //9、该五元组中只有3个玩家的棋子，其他为空，得1800分。（玩家看到赢的希望了，如果电脑没有类似的情况就得堵住这个点，防守防守。【优先级第四】）
        if(_playerChessNum == 3){return 1800;}

        //10、该五元组中只有4个玩家的棋子，其他为空，得100000分。（玩家马上就要赢了，如果电脑要是不能立刻赢的话就必须下在这个点上，快防守啊，黑山老妖要来了。【优先级第二】）
        if(_playerChessNum == 4){return 100000;}

        return 0;
    }

    /**
     * 功能：得到电脑要下棋的位置<br>
     */
    private Map<String,Integer> getComputerLocation()
    {
        //= = = = = = = = = = = = = = = = = = = = 原理 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =//
        //五子棋要赢，必然要有五个棋子在一起成线，那么我们就把这五格相连的线称之为五元组。                                 		//
        //一般情况下棋盘是15×15的，那么应该共有572个五元组。                                                                	//
        //同时，针对五元组中黑子和白子的数量（可以不考虑相对位置）的不同，给每个五元组评不同的分（每组中的5个点都是同样分数）。    //
        //然后每一个位置的得分就是包含这个位置的所有五元组的得分之和。                                                    		//
        //理论上每个落子点最多被包含在20个五元组中，最少被包含在3个五元组中。然后我们循环整个棋盘，找到评分最多且空的那个点，		//
        //就是电脑要落子的那个点。该点可能用于进攻，也可能用于防守。当然以进攻为主，进攻的评分能更高些。							//
        //在本函数中我要对15×15这225个点进行循环，以这个点为龙头，向右、下、45度斜下、135度斜下共4个方向寻找4个五元组				//
        //（边角的点可能在某些方向不构成五元组），并将这些五元组的评分累计到每个五元组中的5个点里。								//
        //由于只是以这个点为龙头去寻找五元组，因此这个点的累计评分不是最终评分，当再以其他点为龙头时可能会包含这个点，会再给这个	//
        //点累计加分，这样全部循环后该点的评分就准了。再循环整个棋盘，找到评分最高且处于空位的那个点就是电脑要下的点。							//
        //问：一共不是有8个方向吗，为什么只找4个方向就行了？																	//
        //答：因为那4个方向是重复计算了。比如说当你以A点为龙头向右寻找五元组时是（A,A+1,A+2,A+3,A+4）这样的五元组。				//
        //	  但当你以A+4这个点位龙头向左寻找五元组时是（A+4,A+3,A+2,A+1,A）这样的五元组，这不就是重复了嘛。						//
        //	  因此我们总结出寻找规律，向右找就别向左找，向下找就别向上找...。													//
        //= = = = = = = = = = = = = = = = = = = = 结束 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =	//

        Map<String,Integer> mapComputer = new HashMap<String,Integer>();
        int computerChessNum;		//五元组中电脑棋子数目累计
        int playerChessNum ;		//五元组中玩家棋子数目累计
        int tupleScore;				//五元组评分值
        int[][] score = new int[this.gamePanel.chessBoradXGrids][this.gamePanel.chessBoradYGrids];	//棋盘中每个落子交叉点的评分数组
        int[][] chess = new int[this.gamePanel.chessBoradXGrids][this.gamePanel.chessBoradYGrids];	//棋盘中每个落子交叉点的棋子数组

        //初始化评分与棋子数组
        for(int i=0;i<this.gamePanel.chessBoradXGrids;i++)
        {
            for(int j=0;j<this.gamePanel.chessBoradYGrids;j++)
            {
                score[i][j] = 0;
                chess[i][j] = 0;
            }
        }
        //为棋子数组赋值，有利于计算且观察。
        for(int i=0;i<this.gamePanel.listChess.size();i++)
        {
            Map<String,Integer> map = this.gamePanel.listChess.get(i);
            chess[ map.get("x")][ map.get("y")] =  map.get("flag");
        }

        //开始循环
        for(int i=0;i<this.gamePanel.chessBoradXGrids;i++)
        {
            for(int j=0;j<this.gamePanel.chessBoradYGrids;j++)
            {
                //1、以该点为龙头向右寻找五元组
                if(i + 4 <= 14)	//这个五元组真的有
                {
                    //初始化变量
                    computerChessNum = 0;
                    playerChessNum = 0;
                    tupleScore = 0;
                    //得到该五元组中电脑与玩家的棋子数目
                    for(int k=0;k<5;k++)
                    {
                        if(chess[i+k][j] == this.gamePanel.computerChess){computerChessNum++;}
                        if(chess[i+k][j] == this.gamePanel.playerChess){playerChessNum++;}
                    }
                    //得到该五元组的评分
                    tupleScore = this.getTupleScore(computerChessNum,playerChessNum);
                    //将该评分赋值给五元组中的5个点
                    for(int k=0;k<5;k++)
                    {
                        score[i+k][j] = score[i+k][j] + tupleScore;
                    }
                }

                //2、以该点为龙头向下寻找五元组
                if(j + 4 <= 14)	//这个五元组真的有
                {
                    //初始化变量
                    computerChessNum = 0;
                    playerChessNum = 0;
                    tupleScore = 0;
                    //得到该五元组中电脑与玩家的棋子数目
                    for(int k=0;k<5;k++)
                    {
                        if(chess[i][j+k] == this.gamePanel.computerChess){computerChessNum++;}
                        if(chess[i][j+k] == this.gamePanel.playerChess){playerChessNum++;}
                    }
                    //得到该五元组的评分
                    tupleScore = this.getTupleScore(computerChessNum,playerChessNum);
                    //将该评分赋值给五元组中的5个点
                    for(int k=0;k<5;k++)
                    {
                        score[i][j+k] = score[i][j+k] + tupleScore;
                    }
                }

                //3、以该点为龙头135度斜下寻找五元组
                if(i + 4 <= 14 && j + 4 <= 14)	//这个五元组真的有
                {
                    //初始化变量
                    computerChessNum = 0;
                    playerChessNum = 0;
                    tupleScore = 0;
                    //得到该五元组中电脑与玩家的棋子数目
                    for(int k=0;k<5;k++)
                    {
                        if(chess[i+k][j+k] == this.gamePanel.computerChess){computerChessNum++;}
                        if(chess[i+k][j+k] == this.gamePanel.playerChess){playerChessNum++;}
                    }
                    //得到该五元组的评分
                    tupleScore = this.getTupleScore(computerChessNum,playerChessNum);
                    //将该评分赋值给五元组中的5个点
                    for(int k=0;k<5;k++)
                    {
                        score[i+k][j+k] = score[i+k][j+k] + tupleScore;
                    }
                }

                //4、以该点为龙头45度斜下寻找五元组
                if(i - 4 >= 0 && j + 4 <= 14)	//这个五元组真的有
                {
                    //初始化变量
                    computerChessNum = 0;
                    playerChessNum = 0;
                    tupleScore = 0;
                    //得到该五元组中电脑与玩家的棋子数目
                    for(int k=0;k<5;k++)
                    {
                        if(chess[i-k][j+k] == this.gamePanel.computerChess){computerChessNum++;}
                        if(chess[i-k][j+k] == this.gamePanel.playerChess){playerChessNum++;}
                    }
                    //得到该五元组的评分
                    tupleScore = this.getTupleScore(computerChessNum,playerChessNum);
                    //将该评分赋值给五元组中的5个点
                    for(int k=0;k<5;k++)
                    {
                        score[i-k][j+k] = score[i-k][j+k] + tupleScore;
                    }
                }
            }
        }

        //从空位置中找到得分最大的位置
        int maxScore = 0;
        int maxX = -1;
        int maxY = -1;
        for(int i=0;i<this.gamePanel.chessBoradXGrids;i++)
        {
            for(int j=0;j<this.gamePanel.chessBoradYGrids;j++)
            {
                if(chess[i][j] == 0)
                {
                    if(maxScore < score[i][j])
                    {
                        maxScore = score[i][j];
                        maxX = i;
                        maxY = j;
                    }
                }
            }
        }

        mapComputer.put("x",maxX);
        mapComputer.put("y",maxY);

        return mapComputer;
    }

    /**
     * 功能：轮到电脑下棋了br>
     */
    public void computerPlay()
    {
        Map<String,Integer> mapComputer = this.getComputerLocation();
        mapComputer.put("flag",this.gamePanel.computerChess);
        //棋盘下满了，电脑无棋可下了
        if(mapComputer.get("x") == -1 || mapComputer.get("y") == -1)
        {
            JOptionPane.showMessageDialog(null,"牛批，你们竟然打平了！");
            this.gamePanel.isGameOver = true;
            this.gamePanel.initGame();
            return;
        }
        if(this.gamePanel.computerChess == this.gamePanel.WHITECHESS)
        {
            this.gamePanel.listChessWhite.add(mapComputer);
        }
        else
        {
            this.gamePanel.listChessBlack.add(mapComputer);
        }
        this.gamePanel.listChess.add(mapComputer);
        //延时一会再刷新，要不显得电脑太厉害了。
        try
        {
            Thread.sleep(50);
        }
        catch(Exception e1)
        {
            e1.printStackTrace();
        }
        this.gamePanel.repaint();
    }

    /**
     * 功能：判断游戏是否结束<br>
     */
    private boolean gameOver()
    {
        int hCount = 0;							//水平计数
        int vCount = 0;							//垂直计数
        int a45Count = 0;						//斜45度计数
        int a135Count = 0;						//斜135度计数
        boolean isFind = false;					//是否找到
        List<Map<String,Integer>> hChessWin = new ArrayList<Map<String,Integer>>();		//记录水平方向上的5个点连成线
        List<Map<String,Integer>> vChessWin = new ArrayList<Map<String,Integer>>();		//记录垂直方向上的5个点连成线
        List<Map<String,Integer>> a45ChessWin = new ArrayList<Map<String,Integer>>();	//记录斜45度方向上的5个点连成线
        List<Map<String,Integer>> a135ChessWin = new ArrayList<Map<String,Integer>>();	//记录斜135度方向上的5个点连成线

        if(this.gamePanel.listChess.size() < 1){return false;}

        //得到最后一步下棋信息
        Map<String,Integer> mapLast = this.gamePanel.listChess.get(this.gamePanel.listChess.size() - 1);
        int x = mapLast.get("x");
        int y = mapLast.get("y");
        int flag = mapLast.get("flag");
        List<Map<String,Integer>> listTmp ;
        if(flag == this.gamePanel.WHITECHESS)
        {
            listTmp = this.gamePanel.listChessWhite;
        }
        else
        {
            listTmp = this.gamePanel.listChessBlack;
        }

        //水平（先向右数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x + i) && map.get("y") == y)
                {
                    hChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                hCount++;
            }
            else
            {
                break;
            }
        }
        //水平（向左数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x - i) && map.get("y") == y)
                {
                    hChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                hCount++;
            }
            else
            {
                break;
            }
        }

        //垂直（向上数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == x && map.get("y") == (y - i))
                {
                    vChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                vCount++;
            }
            else
            {
                break;
            }
        }
        //垂直（向下数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == x && map.get("y") == (y + i))
                {
                    vChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                vCount++;
            }
            else
            {
                break;
            }
        }

        //45度斜线（向右上方数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x + i) && map.get("y") == (y - i))
                {
                    a45ChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                a45Count++;
            }
            else
            {
                break;
            }
        }
        //45度斜线（向左下方数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x - i) && map.get("y") == (y + i))
                {
                    a45ChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                a45Count++;
            }
            else
            {
                break;
            }
        }

        //135度斜线（向左上方数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x - i) && map.get("y") == (y - i))
                {
                    a135ChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                a135Count++;
            }
            else
            {
                break;
            }
        }
        //135度斜线（向右下方数4个）
        for(int i=1;i<5;i++)
        {
            isFind = false;
            for(int j=0;j<listTmp.size();j++)
            {
                Map<String,Integer> map = listTmp.get(j);
                if(map.get("x") == (x + i) && map.get("y") == (y + i))
                {
                    a135ChessWin.add(map);
                    isFind = true;
                }
            }
            if(isFind)
            {
                a135Count++;
            }
            else
            {
                break;
            }
        }

        //开始判断
        if(hCount >= 4 || vCount >= 4 || a45Count >= 4 || a135Count >= 4)
        {
            //记录五子连线
            if(hChessWin.size() == 4)
            {
                hChessWin.add(mapLast);
                this.gamePanel.listChessWin = hChessWin;
            }
            else if(vChessWin.size() == 4)
            {
                vChessWin.add(mapLast);
                this.gamePanel.listChessWin = vChessWin;
            }
            else if(a45ChessWin.size() == 4)
            {
                a45ChessWin.add(mapLast);
                this.gamePanel.listChessWin = a45ChessWin;
            }
            else if(a135ChessWin.size() == 4)
            {
                a135ChessWin.add(mapLast);
                this.gamePanel.listChessWin = a135ChessWin;
            }

            if(this.gamePanel.fightType == 0)	//人机对战
            {
                if(flag == this.gamePanel.computerChess)
                {
                    JOptionPane.showMessageDialog(null,"我去，你怎么连电脑都输啊！","提示",JOptionPane.ERROR_MESSAGE);
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"恭喜，你终于赢电脑一把了！");
                }
                return true;
            }
            else	//人人对战
            {
                if(flag == this.gamePanel.WHITECHESS)
                {
                    JOptionPane.showMessageDialog(null,"恭喜，白棋赢了！");
                }
                else
                {
                    JOptionPane.showMessageDialog(null,"恭喜，黑棋赢了！");
                }
                return true;
            }
        }

        return false;
    }

    /**
     * 功能：悔棋<br>
     */
    public boolean undo()
    {
        if(this.gamePanel.isGameOver){return false;}
        if(this.gamePanel.listChess.size() < 1){return false;}

        //得到最后一步棋信息
        Map<String,Integer> mapLast = this.gamePanel.listChess.get(this.gamePanel.listChess.size() - 1);
        int flag = mapLast.get("flag");
        if(this.gamePanel.fightType == 0)	//人机对战（只有玩家才会悔棋，电脑才不会这么耍赖）
        {
            if(this.gamePanel.chessColor == 0)
            {
                if(this.gamePanel.blackUndoNum == 0)
                {
                    JOptionPane.showMessageDialog(null,"悔棋次数已经全部用完了！","提示",JOptionPane.INFORMATION_MESSAGE);
                    return false;
                }
                this.gamePanel.listChessBlack.remove(this.gamePanel.listChessBlack.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.blackUndoNum--;
            }
            else
            {
                if(this.gamePanel.whiteUndoNum == 0)
                {
                    JOptionPane.showMessageDialog(null,"悔棋次数已经全部用完了！","提示",JOptionPane.INFORMATION_MESSAGE);
                    return false;
                }
                this.gamePanel.listChessWhite.remove(this.gamePanel.listChessWhite.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.whiteUndoNum--;
            }
        }
        else
        {
            if(flag == this.gamePanel.WHITECHESS)
            {
                if(this.gamePanel.whiteUndoNum == 0)
                {
                    JOptionPane.showMessageDialog(null,"白棋的悔棋次数已经全部用完了！","提示",JOptionPane.INFORMATION_MESSAGE);
                    return false;
                }
                this.gamePanel.listChessWhite.remove(this.gamePanel.listChessWhite.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.whiteUndoNum--;
            }
            else
            {
                if(this.gamePanel.blackUndoNum == 0)
                {
                    JOptionPane.showMessageDialog(null,"黑棋的悔棋次数已经全部用完了！","提示",JOptionPane.INFORMATION_MESSAGE);
                    return false;
                }
                this.gamePanel.listChessBlack.remove(this.gamePanel.listChessBlack.size() - 1);
                this.gamePanel.listChess.remove(this.gamePanel.listChess.size() - 1);
                this.gamePanel.blackUndoNum--;
            }
        }

        this.gamePanel.jlb_blackUndoText.setText("剩"+gamePanel.blackUndoNum+"次");
        this.gamePanel.jlb_whiteUndoText.setText("剩"+gamePanel.whiteUndoNum+"次");

        return true;
    }

    /**
     * 功能：鼠标移动事件<br>
     */
    public void mouseMoved(MouseEvent e)
    {
        //判断鼠标位置是否在棋盘内
        int x = e.getX();
        int y = e.getY();
        if(x > (this.gamePanel.chessBoradX - this.gamePanel.spaceSize / 2) && x < (this.gamePanel.chessBoradX + this.gamePanel.chessBoradWidth + this.gamePanel.spaceSize/2) && y > (this.gamePanel.chessBoradY - this.gamePanel.spaceSize / 2) && y < (this.gamePanel.chessBoradY + this.gamePanel.chessBoradHeight + this.gamePanel.spaceSize / 2))
        {
            if(this.gamePanel.isGameOver){return;}
            //清除落子指示器（先不显示）
            this.gamePanel.mapPointer.put("show",0);
            //将x,y由像素改为相应的水平与垂直线的数目（0~14）
            int lineX = this.getLineX(x);
            int lineY = this.getLineY(y);
            if(lineX >= 0 && lineX < 15 && lineY >= 0 && lineY < 15)
            {
                //判断该位置是否有棋子
                boolean isChess = false;
                for(int i=0;i<this.gamePanel.listChess.size();i++)
                {
                    Map<String,Integer> map = this.gamePanel.listChess.get(i);
                    if(map.get("x") == lineX && map.get("y") == lineY)
                    {
                        isChess = true;
                        break;
                    }
                }
                if(!isChess)	//可以显示了
                {
                    this.gamePanel.mapPointer.put("x",lineX);
                    this.gamePanel.mapPointer.put("y",lineY);
                    this.gamePanel.mapPointer.put("show",1);
                }
            }
            this.gamePanel.repaint();
        }
        else
        {
            if(this.gamePanel.mapPointer.get("show") == 1)
            {
                this.gamePanel.mapPointer.put("show",0);
                this.gamePanel.repaint();
            }
        }
    }

    /**
     * 功能：鼠标单击事件<br>
     */
    public void mouseClicked(MouseEvent e)
    {
        if(e.getButton() == MouseEvent.BUTTON1)		//鼠标左键点击
        {
            //判断鼠标位置是否在棋盘内
            int x = e.getX();
            int y = e.getY();
            if(x > (this.gamePanel.chessBoradX - this.gamePanel.spaceSize / 2) && x < (this.gamePanel.chessBoradX + this.gamePanel.chessBoradWidth + this.gamePanel.spaceSize/2) && y > (this.gamePanel.chessBoradY - this.gamePanel.spaceSize / 2) && y < (this.gamePanel.chessBoradY + this.gamePanel.chessBoradHeight + this.gamePanel.spaceSize / 2))
            {
                if(this.gamePanel.isGameOver){return;}
                //将x,y由像素改为相应的水平与垂直线的数目（0~14）
                int lineX = this.getLineX(x);
                int lineY = this.getLineY(y);
                if(lineX >= 0 && lineX < 15 && lineY >= 0 && lineY < 15)
                {
                    //判断该位置是否有棋子
                    boolean isChess = false;
                    for(int i=0;i<this.gamePanel.listChess.size();i++)
                    {
                        Map<String,Integer> map = this.gamePanel.listChess.get(i);
                        if(map.get("x") == lineX && map.get("y") == lineY)
                        {
                            isChess = true;
                            break;
                        }
                    }
                    if(!isChess)	//玩家可以下棋了
                    {
                        //清除落子指示器
                        this.gamePanel.mapPointer.put("show",0);
                        //得到下一步是白棋下还是黑棋
                        int chessColor = this.getNextChessColor();
                        //记录玩家下棋
                        Map<String,Integer> mapMan = new HashMap<String,Integer>();
                        mapMan.put("x",lineX);
                        mapMan.put("y",lineY);
                        mapMan.put("flag",chessColor);
                        if(chessColor == this.gamePanel.WHITECHESS)
                        {
                            this.gamePanel.listChessWhite.add(mapMan);
                            this.gamePanel.jlb_whiteStateText.setText("已下完");
                            this.gamePanel.jlb_blackStateText.setText("思考中");
                        }
                        else
                        {
                            this.gamePanel.listChessBlack.add(mapMan);
                            this.gamePanel.jlb_blackStateText.setText("已下完");
                            this.gamePanel.jlb_whiteStateText.setText("思考中");
                        }
                        this.gamePanel.listChess.add(mapMan);
                        this.gamePanel.repaint();
                        //判断游戏是否结束
                        if(this.gameOver())
                        {
                            this.gamePanel.isGameOver = true;
                            this.gamePanel.setComponentState(false);
                            this.gamePanel.jlb_blackStateText.setText("已结束");
                            this.gamePanel.jlb_whiteStateText.setText("已结束");
                            return;
                        }
                        //判断双方是否战平（棋盘下满了）
                        if(this.gamePanel.listChess.size() >= 225)
                        {
                            JOptionPane.showMessageDialog(null,"牛屁，你们竟然打平了！");
                            this.gamePanel.isGameOver = true;
                            this.gamePanel.initGame();
                            return;
                        }
                        //如果是人机对战，机器要回应啊
                        if(this.gamePanel.fightType == 0)	//人机对战
                        {
                            this.computerPlay();

                            if(this.gamePanel.computerChess == this.gamePanel.BLACKCHESS)
                            {
                                this.gamePanel.jlb_blackStateText.setText("已下完");
                                this.gamePanel.jlb_whiteStateText.setText("思考中");
                            }
                            else
                            {
                                this.gamePanel.jlb_whiteStateText.setText("已下完");
                                this.gamePanel.jlb_blackStateText.setText("思考中");
                            }
                            //判断游戏是否结束
                            if(this.gameOver())
                            {
                                this.gamePanel.isGameOver = true;
                                this.gamePanel.setComponentState(false);
                                this.gamePanel.jlb_blackStateText.setText("已结束");
                                this.gamePanel.jlb_whiteStateText.setText("已结束");
                                return;
                            }
                        }
                    }
                }
            }
        }

    }

}
